package com.cooba.adapter;


import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cooba.R;
import com.cooba.bean.VideoBeans;
import com.cooba.utils.Utils;
import com.lidroid.xutils.BitmapUtils;

import java.util.ArrayList;
import java.util.List;

public class VideoHistoryAdapter extends BaseAdapter {
    private final BitmapUtils bitmapUtils;
    private Context context;
    private List<VideoBeans.VideoListEntity> list=new ArrayList<>();

    public List<VideoBeans.VideoListEntity> getList() {
        return list;
    }

    public void setList(List<VideoBeans.VideoListEntity> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }

    public void addNewData(List<VideoBeans.VideoListEntity> datas) {
        if (datas != null && list != null) {
            this.list.addAll(0, datas);
            this.notifyDataSetChanged();
        }
    }

    public void addMoreData(List<VideoBeans.VideoListEntity> datas) {
        Log.d("wei", "" + datas.size() + "," + list.size());
        if (datas != null && list != null) {
            list.addAll(list.size(), datas);
            this.notifyDataSetChanged();
        }
    }

    public VideoHistoryAdapter(Context context, List<VideoBeans.VideoListEntity> list) {
        this.context = context;
        this.list = list;
        bitmapUtils=new BitmapUtils(context);
        /**
         * 设置默认的图片展现、加载失败的图片展现
         */
        bitmapUtils.configDefaultLoadingImage(R.mipmap.default_icon1);
        bitmapUtils.configDefaultLoadFailedImage(R.mipmap.default_icon1);
    }

    @Override
    public int getCount() {
        if (list==null) {
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.video_history_item, parent, false);
            holder.video_icon = (ImageView) convertView.findViewById(R.id.video_iv_icon);
            holder.video_title = (TextView) convertView.findViewById(R.id.video_tv_tittle);
            holder.video_date = (TextView) convertView.findViewById(R.id.video_tv_date);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();

        }

        bitmapUtils.display(holder.video_icon,list.get(position).getImg_url());
        holder.video_title.setText(Utils.replaceString(list.get(position).getTitle()));
        holder.video_date.setText(list.get(position).getWatch_time());
        return convertView;
    }


    class ViewHolder {
        ImageView video_icon;
        TextView video_title;
        TextView video_date;


    }

}
